 function ghvec=plot_TwoIrfs(irf_mat1,irf_mat2,namesRows,namesPages,namesModels,nameGraph,fonts) 
% =========================================================================
% PLOT_IRF.M 
%  
% ghvec=plot_irf(irf_mat,stpos,shockpos,legends,stnames,shonames,addtotit) 
%
% Given a matrix of IRFS  IRFMAT of dimension [NIRF NPER+1 NX ] 
% NIRF    Number of States 
% NPER    Number of periods 
% NX      Number of pages 
% Note: The dimensions of a 3-dimensional object are referred to as
% row,column and pages. 
% Here the pages correspond to SHOCKS 
% perturbations, plot the IRF to the states indicated in STPOS and to the
% shocks given in SHOCKPOS. Creating one figure for each SHOCK 
% 
% If STPOS (SHOCKPOS) not provided, whole matrix of states (shocks) will be produced 
% 
% Additional inputs 
% ------------------
% LEGENDS       Cell indicating which each line stands for 
% STNAMES       Cell with names of full length state vector (NSTx1) 
% SHONAMES      Cell with names of shocks (NX x 1 ) 
% ADDTOTIT      String to add to the SUPTITLE e.g. STORAGE 
% 
% Output
% GHVEC         Vector with the handles to each figure
%
% Alejandro Justiniano  January 30 2008 
% =========================================================================

% 1. Check dimensions for matrix and assign names to states, shocks and
% legends if not provided 
% =========================================================================
[nst1 nper1 nx1]=size(irf_mat1);nper=nper1-1; 
[nst2 nper2 nx2]=size(irf_mat2);

if nst1~=nst2;error('Rows do not match');end 
if nper1~=nper2;error('Columns do not match');end 
if nx1  ~=nx2   ;error('Pages do not match');end  

if length(namesRows)~=nst1;  error('namesRows  does not match number of rows'); end 
if length(namesPages)~=nx1; error('namesPages does not match number of pages'); end 

if nargin ==8 && ~isempty(axfont);
    set(0,'DefaultAxesFontSize',axfont);
end

% 2. Define General Settings for the PLOTS 
% =========================================================================
style={'-','--',':','-','-*','--.',':*'}; 
width=[ 2.5    2   3  2    2   3     2  ]; 
colormatrix=[0.5    0.5     0.5
             0.0    0.0     0.0]; 
xvec=(0:nper)';

if nst1 < 37
    %     Nrow=ceil(sqrt(nst1));
    %     Ncol=ceil(sqrt(nst1));
    Ncol=5;
    Nrow=ceil(nst1/Ncol);
else
    Ncol = 7;
    Nrow = ceil(nst1/Ncol);
    
end
if nargin ==7 && ~isempty(fonts);
    set(0,'DefaultAxesFontSize',fonts.axes);
    titfont=fonts.titfont;
else
    if Nrow < 5
        titfont=12; %Font for titles
    elseif Nrow < 8
        titfont=10;
    else
        titfont=8;
    end
end
% 3. One figure per shock, subfigure per state
% =========================================================================
tolSmall=1e-6; 
ghvec=zeros(nst1,1);
for ii=1:nx1;
    
    ghvec(ii)=figure;
    
    for jj=1:nst1

        subplot(Nrow,Ncol,jj);
        
        % Makes small numbers zero 
        Y1=squeeze(irf_mat1(jj,:,ii));
        Y2=squeeze(irf_mat2(jj,:,ii));
        flatten1=find(abs(Y1) < tolSmall);
        if ~isempty(flatten1)
            Y1(flatten1)=zeros(length(flatten1),1);
        end
        flatten2=find(abs(Y2) < tolSmall);
        if ~isempty(flatten2)
            Y2(flatten2)=zeros(length(flatten2),1);
        end
                
        hand=plot(xvec,Y1,style{1},'LineWidth',width(1));
        set(hand,'Color',colormatrix(1,:));
        hold on; 
        hand=plot(xvec,Y2,style{2},'LineWidth',width(2));
        set(hand,'Color',colormatrix(2,:));
        
        % Title and Legend
        title(namesRows{jj},'FontSize',titfont);
        set(gca,'Box','off')
        xlim([0 nper]); 
        %set(gca,'XTick',[0 nper])
        axis tight
        edgeoff=0.01;
        yLimvec=[min( [Y1(:);Y2(:);0] )-edgeoff...
            max( [Y1(:);Y2(:);0] )+edgeoff];
        ylim([yLimvec(1) yLimvec(2)]);
        
        plot(xvec,zeros(length(xvec)),'r')
        
        %         if sum(Y1<0) ~= 0 || sum(Y2<0) ~= 0
        %         ystuff      = get(gca,'yLim');
        %         newylim     = [-max(abs(ystuff)) max(abs(ystuff))];
        %         ylim(newylim)
        %         plot(xvec,zeros(length(xvec)),'r')
        %         end
        %
    end    
    handleg=legend([220 10 0.4 0.4],namesModels,'orientation','horizontal','FontSize',11); 
    set(handleg,'Box','Off'); 
    
        set(ghvec(ii),'PaperPosition',[-0.0 -0.0 8.75 10.75])
    hand=suptitle([namesPages{ii},' IRF ',nameGraph]);
    set(hand,'FontSize',titfont+4); 
    
end
set(0,'DefaultAxesFontSize','remove');